/* Player is defusing the bomb */
/* TODO: Give the player some animation to prevent him from shooting (perhaps a cancel option too?) */

/* These variables should probably be set somewhere else */
disableSerialization;
_maxTime = 15;  // seconds
_current = 0;
_waitFactor = 0.5;

_startTime = time;
scopeName "defuseMain";

/* Check if player has a def kit */
if ("ACE_JerryCan_Dummy_15" in  (weapons player)) then {
	_maxTime = round(_maxTime / 2);
};

_ctrl = (_this select 0) DisplayCtrl 11001;
_ctrlPos = ctrlPosition _ctrl;

/* Lock the bomb from being defused by anyone else */
bombLocked = true;
publicVariable "bombLocked";

while {(dialog) AND (alive player)} do {	
	if (time >= (_startTime + _maxTime)) then {
		/* Defuse complete */
		hint "completed!";

		closeDialog 0;
		ROUND_WIN = 1;
		publicVariable "ROUND_WIN";
		[] call fnc_eh_win;
		
		breakTo "defuseMain";
	} else {
		_d = time - _startTime;

		_ctrl ctrlSetPosition[_ctrlPos select 0,_ctrlPos select 1, (_d / _maxTime)*(_ctrlPos select 2),_ctrlPos select 3];
		_ctrl ctrlSetText format["%1%2", round((_d / _maxTime) * 100), "%"];
		_ctrl ctrlCommit 0.0;

		sleep 0.1;
	};
};

// Incase we got shot!
if (dialog) then {
	closeDialog 0;
};

/* Release the lock */
bombLocked = false;
publicVariable "bombLocked";